package com.demo.seckill.common.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * @author libo
 * @ClassName RedisUtils
 * @Description: Redis工具类
 * @date 2018/11/15 18:41
 */
@Component
public class RedisUtils {
    private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);

    @Resource
    private RedisTemplate<Serializable,Serializable> redisTemplate;

    /**
     * 前缀
     */
    public static final String KEY_PREFIX_VALUE = "demo:seckill:value:";

    public boolean cacheValue(String k, Serializable v, long time){
        String key = KEY_PREFIX_VALUE + k;
        try {
            ValueOperations<Serializable, Serializable> valueOps =  redisTemplate.opsForValue();
            valueOps.set(key,v);
            if(time > 0){
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return  true;
        }catch (Throwable t ){
            logger.error("缓存[{}]失败, value[{}]",key,v,t);
        }
        return  false;
    }

    public  boolean cacheValue(String k, Serializable v, long time,TimeUnit unit) {
        String key = KEY_PREFIX_VALUE + k;
        try {
            ValueOperations<Serializable, Serializable> valueOps =  redisTemplate.opsForValue();
            valueOps.set(key, v);
            if (time > 0) redisTemplate.expire(key, time, unit);
            return true;
        } catch (Throwable t) {
            logger.error("缓存[{}]失败, value[{}]",key,v,t);
        }
        return false;
    }
    /**
     * 缓存value操作
     * @param k
     * @param v
     * @return
     */
    public  boolean cacheValue(String k, Serializable v) {
        return cacheValue(k, v, -1);
    }

    /***
     * @Author libo
     * @Description 判断缓存是否存在
     * @Date 18:49 2018/11/15
     * @return
     */
    public boolean containsValueKey(String k){
        String key = KEY_PREFIX_VALUE + k;
        try {
            return redisTemplate.hasKey(key);
        } catch (Throwable t) {
            logger.error("判断缓存存在失败key[" + key + ", error[" + t + "]");
        }
        return false;
    }

    /***
     * @Author libo
     * @Description 获取缓存
     * @Date 18:51 2018/11/15
     * @return 
     */
    public Serializable getValue(String k){
        String key = KEY_PREFIX_VALUE + k;
        try{
            ValueOperations<Serializable, Serializable> valueOps =  redisTemplate.opsForValue();
            return valueOps.get(KEY_PREFIX_VALUE + k);
        }catch (Throwable t){
            logger.error("获取缓存失败key[" + KEY_PREFIX_VALUE + k + ", error[" + t + "]");
        }
        return  null;
    }

    /***
     * @Author libo
     * @Description //移除缓存
     * @Date 18:53 2018/11/15
     * @return 
     */
    public boolean removeValue(String k){
        String key = KEY_PREFIX_VALUE + k;
        try {
            redisTemplate.delete(k);
            return true;
        }catch (Throwable t){
            logger.error("删除缓存失败key[" + KEY_PREFIX_VALUE + k + ", error[" + t + "]");
        }
        return  false;
    }

}

